﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{
    public class SalesController : Controller
    {
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();
        //
        // GET: /Sales/

        public ActionResult Index()
        {
            return View();
        }

        #region 获取当前操作员拥有商户的权限
        public ActionResult getMer()
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];//获取oper

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                List<Model.View_Order_MerSet> List = new List<Model.View_Order_MerSet>();
                string result = wsdcser.GetMer(user.OperID, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_MerSet>.JsonList(jieguo);//解析返回的结果
                    return Json(List);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 商品分类下拉框
        public ActionResult getOrderType(string MerID) 
        {
            try
            {
                List<Model.Order_DishesType_T> List = new List<Model.Order_DishesType_T>();
                string result = wsdcser.GetOrder_DishesType(MerID, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.Order_DishesType_T>.JsonList(jieguo);//解析返回的结果
                    return Json(List);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 获取当前上架列表
        /// <summary>
        /// 
        /// </summary>
        /// <param name="MerID"></param>
        /// <returns></returns>
        public ActionResult GetShelves(string MerID)
        {
            try
            {
                Model.ShelvesInfo List = new ShelvesInfo();
                string result = wsdcser.GetShelvesInfo(MerID, GetYZM._YZM, 1);
                List<ShelvesInformation> newList = new List<ShelvesInformation>();
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    //List = ParseClass<Model.ShelvesInfo>.JsonList(jieguo);//解析返回的结果
                    List<Model.ShelvesInfo> ShelvesInfo = ParseClass<List<Model.ShelvesInfo>>.JsonDeserialize(searcho["rows"].ToString());


                    foreach (Model.ShelvesInfo item in ShelvesInfo)
                    {
                        ShelvesInformation aa = new ShelvesInformation();
                        aa.DishesID = item.DishInfo.DishesID;
                        aa.DelPackMon = item.DishInfo.DelPackMon;
                        aa.DishesEnableInt = item.DishInfo.DishesEnableInt;
                        aa.DishesEvaNun = item.DishInfo.DishesEvaNun;
                        aa.DishesEvaTxt = item.DishInfo.DishesEvaTxt;
                        aa.DishesEvaX = item.DishInfo.DishesEvaX;
                        aa.DishesGood = item.DishInfo.DishesGood;
                        aa.DishesIsInt = item.DishInfo.DishesIsInt;
                        aa.DishesMon = item.DishInfo.DishesMon;
                        aa.DishesNmae = item.DishInfo.DishesNmae;
                        aa.DishesPay = item.DishInfo.DishesPay;
                        aa.DishesPic = item.DishInfo.DishesPic;
                        aa.DishesTxt = item.DishInfo.DishesTxt;
                        aa.DishesTypeID = item.DishInfo.DishesTypeID;
                        aa.DishesTypeState = item.DishInfo.DishesTypeState;
                        aa.DishesTypeName = item.DishInfo.DishesTypeName; 
                        aa.DishesVol = item.DishInfo.DishesVol;
                        aa.HaveNub = item.HaveNub;
                        aa.Sold = item.HaveNub - item.SaleNub;
                        aa.SaleNub = item.SaleNub;
                        newList.Add(aa);
                    }
                    //newList = ParseClass<Model.Order_Dishes_T>.JsonList(ShelvesInfo)
                    var data = new
                    {
                        total = ShelvesInfo.Count,
                        rows = newList
                    };
                    
                     return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = 0,
                        rows = newList
                    };

                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 售罄

        public ActionResult SoldOut(string DishesID, int HasNub)
        {
            try
            {
                string result = wsdcser.SetOrder_Shel(DishesID, HasNub, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(bs);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 查询
        public ActionResult Search(string MerID, string DishesTypeID, string DishesNmae)
        {
            string result = wsdcser.GetShelvesInfo(MerID, GetYZM._YZM, 1);
            List<ShelvesInformation> newList = new List<ShelvesInformation>();
            JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
            string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
            if (bs == "1")//返回结果正常
            {
                IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                //List = ParseClass<Model.ShelvesInfo>.JsonList(jieguo);//解析返回的结果
                List<Model.ShelvesInfo> ShelvesInfo = ParseClass<List<Model.ShelvesInfo>>.JsonDeserialize(searcho["rows"].ToString());


                foreach (Model.ShelvesInfo item in ShelvesInfo)
                {
                    ShelvesInformation aa = new ShelvesInformation();
                    aa.DishesID = item.DishInfo.DishesID;
                    aa.DelPackMon = item.DishInfo.DelPackMon;
                    aa.DishesEnableInt = item.DishInfo.DishesEnableInt;
                    aa.DishesEvaNun = item.DishInfo.DishesEvaNun;
                    aa.DishesEvaTxt = item.DishInfo.DishesEvaTxt;
                    aa.DishesEvaX = item.DishInfo.DishesEvaX;
                    aa.DishesGood = item.DishInfo.DishesGood;
                    aa.DishesIsInt = item.DishInfo.DishesIsInt;
                    aa.DishesMon = item.DishInfo.DishesMon;
                    aa.DishesNmae = item.DishInfo.DishesNmae;
                    aa.DishesPay = item.DishInfo.DishesPay;
                    aa.DishesPic = item.DishInfo.DishesPic;
                    aa.DishesTxt = item.DishInfo.DishesTxt;
                    aa.DishesTypeID = item.DishInfo.DishesTypeID;
                    aa.DishesTypeState = item.DishInfo.DishesTypeState;
                    aa.DishesTypeName = item.DishInfo.DishesTypeName;
                    aa.DishesVol = item.DishInfo.DishesVol;
                    aa.HaveNub = item.HaveNub;
                    aa.Sold = item.HaveNub - item.SaleNub;
                    aa.SaleNub = item.SaleNub;
                    newList.Add(aa);
                }
                //newList = ParseClass<Model.Order_Dishes_T>.JsonList(ShelvesInfo)
                //var data = new
                //{
                //    total = ShelvesInfo.Count,
                //    rows = newList
                //};
                //return Json(newList);
            }
            else
            {
                return null;
            }

            if (DishesTypeID != "" || DishesNmae != "")
            {
                if (DishesTypeID == "-8989")
                {
                    newList = newList.ToList();
                }
                if (DishesTypeID != "" && DishesTypeID != "-8989")
                {
                    newList = newList.Where(i => i.DishesTypeID.Contains(DishesTypeID)).ToList();
                }
                if (DishesNmae != "")
                {
                    newList = newList.Where(i => i.DishesNmae.Contains(DishesNmae)).ToList();
                }
                
                var data = new
                {
                    total = newList.Count,
                    rows = newList//返回当前页的数据 
                };
                return Json(data, JsonRequestBehavior.AllowGet);
            }

            else
            {
                var data = new
                {
                    total = newList.Count,
                    rows = newList//返回当前页的数据 
                };
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        public class ShelvesInformation
        {
            public int HaveNub { get; set; }
            public int Sold { get; set; }
            public int SaleNub { get; set; }
            public double DelPackMon { get; set; }
            public int DishesEnableInt { get; set; }
            public int DishesEvaNun { get; set; }
            public int DishesEvaTxt { get; set; }
            public int DishesEvaX { get; set; }
            public int DishesGood { get; set; }
            public string DishesID { get; set; }
            public int DishesIsInt { get; set; }
            public double DishesMon { get; set; }
            public string DishesNmae { get; set; }
            public double DishesPay { get; set; }
            public string DishesPic { get; set; }
            public string DishesTxt { get; set; }
            public string DishesTypeID { get; set; }
            public int DishesTypeState { get; set; }
            public string DishesTypeName { get; set; }
            public int DishesVol { get; set; }
            public string IntBZ { get; set; }
            public string MerID { get; set; }
        }
    }
}
